﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Autofac;
using TbShop.Web.Infrastructure;
using HeinQi.Framework.Common.Caching;
using TbShop.Business.Components;
using TbShop.Web.Areas.Admin.Models;

namespace TbShop.Web.Helpers
{
    public class SiteMapHelper
    {
        public static IEnumerable<SiteMapViewModels> LoadSiteMap()
        {
            var key = StaticWords.CacheKeyForSiteMap;
            IEnumerable<SiteMapViewModels> sites = SessionHelper.Get<IEnumerable<SiteMapViewModels>>(key);
            if (sites == null || !sites.Any())
            {
                SiteMapBC sitemapProxy = new SiteMapBC();
                var userId = SessionHelper.Get(StaticWords.SessionUserIdKey);
                var siteEntity = sitemapProxy.GetAllSiteMap(userId == null ? "" : userId.ToString());
                sites = DataMapperHelper.MapList<SiteMapViewModels>(siteEntity);
                if (sites.Any())
                {
                    for (var i = 0; i < siteEntity.Count(); i++)
                    {
                        sites.ElementAt(i).ChildSiteMapViewModels = DataMapperHelper.MapList<ChildSiteMaps>(siteEntity.ElementAt(i).ChildSiteMaps);
                    }
                }
                SessionHelper.Set(key, sites);
            }
            return sites;
        }
    }
}